package com.bw.springcloud.service;

import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.concurrent.TimeUnit;

/**
 * 类描述
 *
 * @author Mr.Wu
 * @create 2020-05-08
 */
@Service
public class PaymentService {

	public String paymentInfo_OK(Integer id) {
		return "线程池： " + Thread.currentThread().getName() + "   paymentInfo_OK,id:" + id + " 正常访问！";
	}

	@HystrixCommand(fallbackMethod = "paymentInfo_TimeoutHandler",
			commandProperties = {
				// 指定3秒以内的都算正常
				@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000")
			})
	public String paymentInfo_Timeout(Integer id) {
		// 模拟复杂的业务流程
//		int  i = 10/0;
		int timeNumber = 5;
		try {
			TimeUnit.SECONDS.sleep(timeNumber);
		} catch (Exception e){
			e.printStackTrace();
		}
//		return "线程池： " + Thread.currentThread().getName() + "   paymentInfo_OK,id:" + id + " 耗时(秒):" + timeNumber;
		return "线程池： " + Thread.currentThread().getName() + "   paymentInfo_OK,id:" + id + " 耗时(秒):" + timeNumber;
	}

	public String paymentCircuitBreaker(@PathVariable("id") Integer id) {
		if(id < 0){
			throw  new RuntimeException("****id 不能为负数");
		}
		// 使用hutool工具来生成uuid
		String serialNumber = IdUtil.simpleUUID();

		return  Thread.currentThread().getName() + "\t" + "调用成功，流水号：" + serialNumber;
	}


	public String paymentInfo_TimeoutHandler(Integer id){
		return "/(ToT)/调用8001支付接口超时或异常！！！！、\t" + "\t当前线程池名字" + Thread.currentThread().getName()+"id:"+id;
	}
}
